package zion830.com.range_picker_dialog

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.FragmentActivity
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import zion830.com.range_picker_dialog.databinding.TimeRangePickerBottomSheetDialogBinding

/**
 * TimeRangePicker based on BottomSheetDialogFragment.
 * If you want to use TimeRangePickerBottomSheet, you need to add Material Components for Android.
 * Note the following link : https://material.io/develop/android/docs/getting-started/
 */
class TimeRangePickerBottomSheet : BottomSheetDialogFragment() {
    private lateinit var binding: TimeRangePickerBottomSheetDialogBinding

    // Listener called when time is selected
    var onTimeRangeSelectedListener: OnTimeRangeSelectedListener? = null

    var timeRange: TimeRange? = null

    // Minute time interval. default value is 1
    var interval = TimeRangePicker.minInterval

    var oneDayMode = true

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = TimeRangePickerBottomSheetDialogBinding.inflate(inflater)
        initView()
        setTimeRange()
        return binding.root
    }

    fun show(fragmentActivity: FragmentActivity) {
        show(fragmentActivity.supportFragmentManager, TAG)
    }

    fun showPicker(fragmentActivity: FragmentActivity,selectedListener: OnTimeRangeSelectedListener){
        this.onTimeRangeSelectedListener = selectedListener
        show(fragmentActivity)
    }


    private fun initView() {
        with(binding) {
            tpStart.setIs24HourView(true)
            tpEnd.setIs24HourView(true)
            tpStart.timeInterval = interval
            tpEnd.timeInterval = interval
            btnOk.setOnClickListener {
                onTimeRangeSelectedListener?.onTimeSelected(getSelectedTimeRange())
                dismiss()
            }
        }
    }

    class Builder : Buildable<TimeRangePickerBottomSheet>, TimeRangeSelectable<Builder> {
        private var listener: OnTimeRangeSelectedListener? = null
        private var defaultTimeInterval = TimeRangePicker.minInterval
        private var defaultTimeRange = TimePickerUtils.getCurrentTimeRange()
        private var defaultOnDayMode = false

        override fun setOnDayMode(OnDayMode: Boolean): Builder {
            defaultOnDayMode = OnDayMode
            return this
        }

        override fun setTimeInterval(timeInterval: Int): Builder {
            defaultTimeInterval = timeInterval
            return this
        }

        override fun setTimeRange(timeRange: TimeRange): Builder {
            defaultTimeRange = timeRange
            return this
        }

        override fun setOnTimeRangeSelectedListener(timeRangeSelectedListener: OnTimeRangeSelectedListener): Builder {
            listener = timeRangeSelectedListener
            return this
        }

        override fun build(): TimeRangePickerBottomSheet {
           return TimeRangePickerBottomSheet().apply {
               onTimeRangeSelectedListener = listener
               interval = defaultTimeInterval
               oneDayMode = defaultOnDayMode
               timeRange = defaultTimeRange
           }
        }
    }

    private fun getSelectedTimeRange() = with(binding) {
        TimeRange(
            tpStart.getDisplayedHour(),
            tpStart.getDisplayedMinute(),
            tpEnd.getDisplayedHour(),
            tpEnd.getDisplayedMinute()
        )
    }

    private fun setTimeRange() {
        with(binding) {
            tpStart.setDisplayedHour(timeRange?.startHour?:1)
            tpStart.setDisplayedMinute(timeRange?.startMinute?:0)
            tpEnd.setDisplayedHour(timeRange?.endHour?:1)
            tpEnd.setDisplayedMinute(timeRange?.endMinute?:0)
        }
    }

    companion object {
        private val TAG = TimeRangePickerBottomSheet::class.java.name

        fun getInstance(): TimeRangePickerBottomSheet {
            val pickBottomSheetFragment = TimeRangePickerBottomSheet()
            return pickBottomSheetFragment
        }
    }
}